package com.wtgroup.demo.leetcode.q011_盛最多水的容器;

/**
 * @author dafei
 * @version 0.1
 * @date 2021/3/27 13:58
 */
public class S_DoublePointer {

    public static void main(String[] args) {
        int[][] inputs = {
                {1,8,6,2,5,4,8,3,7},
                {1,1},
                {4,3,2,1,4},
                {1,2,1}
        };
        for (int[] input : inputs) {
            System.out.println(new S_DoublePointer().maxArea(input));
        }
    }

    /**
     * 官方: 双指针
     *
     */
    public int maxArea(int[] height) {
        int lp = 0;
        int rp = height.length - 1;
        int area = 0;
        while (lp < rp) {
            int cur = Math.min(height[lp], height[rp]) * (rp - lp);
            if (cur > area) area = cur;
            // 矮的一侧要移动
            if (height[lp] <= height[rp]) lp++; else rp--;
        }

        return area;
    }
}
